<template>


<div>
  <div style="padding: 10px 0">
    <el-input style="width: 200px"  placeholder="请输入名称" suffix-icon="el-icon-search" v-model="stuName"></el-input>
    <el-input style="width: 200px"  placeholder="请输入手机号" suffix-icon="el-icon-search" v-model="mobile"></el-input>
    <el-input style="width: 200px"  placeholder="请输入身份证号" suffix-icon="el-icon-search" v-model="idNum"></el-input>
    <el-select style="width: 200px; margin-left: 5px" v-model="sex" clearable placeholder="请选择">
      <el-option
        v-for="item in sexs"
        :key="item.sex"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>

    <el-button type="primary" icon="el-icon-search" @click="load">搜索</el-button>
    <el-button type="warning" icon="el-icon-search" @click="clear">清空</el-button>
  </div>

  <div style="margin: 10px 0">
    <el-button type="primary" @click="handAdd()">新增<i class="el-icon-circle-plus-outline"></i></el-button>

    <el-popconfirm
      confirm-button-text='好的'
      cancel-button-text='不用了'
      icon="el-icon-info"
      icon-color="red"
      title="你确定批量删除吗？"
      @confirm="delsDuo()"
    >
      <el-button type="danger"  slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>
    </el-popconfirm>

    <el-upload  style="display:inline-block" action="http://localhost:8088/student/import" :show-file-list="false" accept="xlsx" :on-success="imSuccess">
    <el-button type="primary">导入 <i class="el-icon-circle-plus-outline"></i></el-button>
    </el-upload>


    <el-button type="primary" @click="exportss">导出 <i class="el-icon-circle-plus-outline"></i></el-button>
  </div>

  <el-table :data="tableData"  @selection-change="handleSelectionChange">>
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column prop="stuId" label="ID" >
    </el-table-column>
    <el-table-column prop="stuNum" label="学号" >
    </el-table-column>
    <el-table-column prop="stuName" label="学生姓名" >
    </el-table-column>
    <el-table-column prop="mobile" label="手机号">
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template slot-scope="scope">
        <span v-if="scope.row.sex === 'M'">男</span>
        <span v-if="scope.row.sex === 'F'">女</span>
      </template>
    </el-table-column>
    <el-table-column prop="age" label="年龄">
    </el-table-column>
    <el-table-column prop="idType" label="证件类型">
      <template slot-scope="scope">
      <span v-if="scope.row.idType === '1'" >身份证</span>
      <span v-if="scope.row.idType === '2'" >学生证</span>
      <span v-if="scope.row.idType === '3'" >其他</span>
      </template>
    </el-table-column>
    <el-table-column prop="idNum" label="证件号码">
    </el-table-column>
    <el-table-column prop="birthday" label="出生日期">
    </el-table-column>
    <el-table-column prop="nativePlace" label="籍贯">
    </el-table-column>
<!--    <el-table-column prop="createDatee" label="创建日期">
    </el-table-column>
    <el-table-column prop="updateDate" label="修改日期">
    </el-table-column>-->

    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button type="success" @click="update(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
        <el-popconfirm
          confirm-button-text='好的'
          cancel-button-text='不用了'
          icon="el-icon-info"
          icon-color="red"
          title="你确定删除吗？"
          @confirm="del(scope.row.stuId)"
        >

          <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </template>
    </el-table-column>

  </el-table>

  <div style="padding: 10px 0">
    <el-pagination

      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"

      :current-page="pageNum"
      :page-sizes="[3, 6, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>


  <el-dialog title="个人信息" :visible.sync="dialogFormVisible" width="500px">
    <el-form label-width="70px">
      <el-form-item label="学号">
        <el-input v-model="form.stuNum" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.stuName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.mobile" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别">
          <el-radio v-model="form.sex" label="F">女</el-radio>
          <el-radio v-model="form.sex" label="M">男</el-radio>
      </el-form-item>
      <el-form-item label="年龄">
<!--                                                    readonly:只读属性 -->
        <el-input v-model="form.age" autocomplete="off" readonly></el-input>
      </el-form-item>
      <el-form-item label="证件类型">
        <el-radio v-model="form.idType" label="1">身份证</el-radio>
        <el-radio v-model="form.idType" label="2">学生证</el-radio>
        <el-radio v-model="form.idType" label="3">其他</el-radio>
      </el-form-item>
      <el-form-item label="证件号">
        <el-input v-model="form.idNum" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="生日">
        <el-date-picker
          v-model="form.birthday"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <br>

      <el-form-item label="籍贯" prop="selectedOptions">
        <el-cascader
          size="large"
          :options="options"
          v-model="selectedOptions"
          @change="handleChange"
          style="width: 350px"
        >
        </el-cascader>
      </el-form-item>

<!--      <el-form-item label="籍贯">
        <el-input v-model="form.detailAddr" autocomplete="off"></el-input>
      </el-form-item>-->

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="adds()">确 定</el-button>
      <el-button @click="dialogFormVisible = false">取 消</el-button>
    </div>
  </el-dialog>


</div>


</template>

<script>

//引入地址插件
import {  regionData,CodeToText,TextToCode } from 'element-china-area-data'
export default {
  name: "User",

  data(){
    return{


      pickerOptions: { //日期带快捷选项
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      value1: '',
      value2: '',

      //省市县全部数据
      options: regionData,
      //选中的省市县区域码
      selectedOptions: [], //

      tableData: [],
      total:0,
      pageNum:1,
      pageSize:3,
      stuName:'', //模糊查要加的
      sex: '',
      idNum: '',
      mobile: '',
      dialogFormVisible:false, //添加所用
      form:{},

      multipleSelection:[],//多选删除所用

      sexs: [
        {value: 'M', label: '男'},
        {value: 'F', label: '女'}
      ],
    }

  },

  created() {
    this.load()

  },
  methods:{


    load(){
      //普通的分页+模糊查写法
      /*        fetch("http://localhost:8088/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&name="+this.name).then(res => res.json()).then(res => {
                console.log(res)
                this.tableData=res.data
                this.total= res.total
              })*/

      //mybatis-Plus分页加模糊查写法 用到axios

      // 原url :http://localhost:8088/animal/page 在request.js中配置了前缀
      this.request.get("/student/page",{
        params:{
          pageNum:this.pageNum,
          pageSize: this.pageSize,
          stuName:this.stuName,
          sex:this.sex,
          mobile:this.mobile,
          idNum:this.idNum,
        }

      }).then(res=>{

        console.log(res)
        this.tableData =res.records
        this.total =res.total
      })


    },
    handleSizeChange(pageSize){
      this.pageSize= pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
    },

    clear(){ //清空模糊查的数据
      this.stuName=""
      this.sex=""
      this.mobile=""
      this.idNum=""
      this.load()
    },

    handAdd(){ //点击添加按钮后弹出对话框
      this.dialogFormVisible =true
      this.form={}
    },

    adds(){
      this.request.post("/student/addAndUpdate",this.form).then(res=>{
        if(res.code === '200'){
          this.$message.success("保存成功")
          this.dialogFormVisible=false //保存成功后关闭对话框
          this.load()
        }else {
          this.$message.error("保存失败")
        }
      })
    },

    update(row){
      this.form = JSON.parse(JSON.stringify(row))
      //省市回显
      this.selectedOptions = [this.form.provincesId, this.form.citiesId, this.form.areasId]
      this.dialogFormVisible =true
    },

    del(stuId){
      this.request.delete("/student/"+stuId).then(res=>{
        if(res.code === '200'){
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },

    handleSelectionChange(val){//批量删除的多选框
      this.multipleSelection= val
    },
    delsDuo(){
      let  ids=this.multipleSelection.map(v =>v.stuId) //[{}，{},{}] 改变成 [1,2,3]
      this.request.post("/student/deleteDuo",ids).then(res=>{
        if(res.code === '200'){
          this.$message.success("多选删除成功")
          this.load()
        }else {
          this.$message.error("多选删除失败")
        }
      })

    },
    exportss(){
      window.open("http://localhost:8088/student/export")
    },
    imSuccess(){
      this.$message.success("文件导入成功")
      this.load()
    },

    //省市区三级联动事件
    handleChange (value) {
      // this.edit();
      //console.log(value)
      if (value[1] != null && value[2] != null) {
        var dz = CodeToText[value[0]] + '/' + CodeToText[value[1]] + '/' + CodeToText[value[2]]

      } else {
        if (value[1] != null) {
          dz = CodeToText[value[0]] + '/' + CodeToText[value[1]]
        } else {
          dz = CodeToText[value[0]]
        }
      }
      this.form.nativePlace = dz

      this.form.provincesId= this.selectedOptions[0]
      this.form.citiesId= this.selectedOptions[1]
      this.form.areasId= this.selectedOptions[2]
    },


  }

}
</script>

